import 'package:base_lib/common/base/base_bloc_page.dart';
import 'package:base_lib/common/index.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/router/routers.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:base_lib/utils/log.dart';
import 'package:doctor_examine_lib/doctor_examine_componet.dart';
import 'package:doctor_login_lib/cache/userinfo_cache.dart';
import 'package:doctor_order_lib/component.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_component.dart';
import 'package:doctor_user_lib/doctor_user_component.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:doctor_login_lib/login/entity/doctor_login_entity.dart'
    as LoginBean;
import 'bloc.dart';

class MinePage extends BaseBlocPage<MineBloc, _MinePageState> {
  MinePage(Parameters parameters) : super(parameters);

  @override
  _MinePageState generateThemeState() {
    return _MinePageState();
  }
}

class _MinePageState extends BaseBlocPageState<MineBloc> {
  var tsWhite = TextStyle(fontSize: 16, color: Colors.white);
  var tsBlackBig = TextStyle(fontSize: 16, color: Colors.black);
  var tsBlack = TextStyle(fontSize: 14, color: Colors.black);

  var menuList = <_MenuList>[];

  @override
  void initState() {
    super.initState();

    menuList
      ..clear()
      ..addAll([
        _MenuList('images/mine/zhuanzhen.png', '我的转诊', () {
          ToastUtil.info('功能开发中');
        }),
        _MenuList('images/mine/jianchajianyan.png', '检查检验', () {
          ToastUtil.info('功能开发中');
        }),
        _MenuList('images/mine/chufangdingdan.png', '处方订单', () {
          Routers.navigateTo(
              context, DoctorOrderComponent.prescriptionOrderPage);
        }),
      ]);

    UserInfoCache.getInstance().get().then((value) {
      bloc.userInfo = value;
      setState(() {});
    });
  }

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
        backgroundColor: theme.themeColor.themeBgColor,
        appBar: CustomAppbar(
          contentHeight: 0,
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                width: ScreenAdapter.screenWidthDp,
                height: ScreenAdapter.screenWidthDp * 0.52,
                child: Stack(
                  children: [
                    Image.asset(
                      'images/mine/bg.png',
                      fit: BoxFit.fill,
                      // color: theme.themeColor.themeColor,
                      // colorBlendMode: BlendMode.multiply,
                      width: double.infinity,
                    ),
                    Positioned(
                        top: 10,
                        left: 0,
                        height: 30,
                        width: ScreenAdapter.screenWidthDp,
                        child: Row(
                          children: [
                            Spacer(),
                            Center(
                              child: Text(
                                '个人中心',
                                style: tsWhite.copyWith(fontSize: 20),
                              ),
                            ),
                            Expanded(
                                child: Align(
                              alignment: Alignment.centerRight,
                              child: GestureDetector(
                                  onTap: () {
                                    Routers.navigateTo(context,
                                        DoctorUserComponent.setSettingPage);
                                  },
                                  child: Container(
                                    padding:
                                        EdgeInsets.only(left: 10, right: 10),
                                    child: Image.asset(
                                      'images/mine/setting.png',
                                      color: Colors.white,
                                      height: 20,
                                      width: 20,
                                    ),
                                  )),
                            )),
                          ],
                        )),
                    Positioned(
                      left: 10,
                      top: (ScreenAdapter.screenWidthDp * 0.52) / 2 - 20,
                      child: Container(
                        // height: 60,
                        // width: 60,
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            ClipRRect(
                              child: FadeInImage.assetNetwork(
                                placeholder: 'images/doctor.png',
                                image: bloc.userInfo?.faceImage ?? '',
                                height: 60,
                                width: 60,
                              ),
                              borderRadius: BorderRadius.circular(30),
                            ),
                            10.hGapDp,
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  bloc?.userInfo?.doctorName ?? '',
                                  style: tsWhite,
                                ),
                                5.vGapDp,
                                Text(
                                  bloc?.userInfo?.doctorInfo?.titleName ?? '',
                                  style: tsWhite,
                                ),
                              ],
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                width: double.infinity,
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: Text(
                  '患者服务',
                  style: tsBlackBig,
                ),
              ),
              1.hGapDp,
              Container(
                width: double.infinity,
                padding: EdgeInsets.all(10),
                color: Colors.white,
                child: _buildMenu(theme),
              ),
            ],
          ),
        ));
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    FLog('动 didChangeDependencies' * 5);
  }

  @override
  void didUpdateWidget(covariant StatefulWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    FLog('动 didUpdateWidget' * 5);
  }

  @override
  MineBloc generateBloc() {
    return MineBloc();
  }

  ///患者服务菜单
  _buildMenu(MTheme theme) {
    var widgets = <Widget>[];

    menuList.forEach((element) {
      var w = GestureDetector(
        onTap: element.function,
        child: Container(
          padding: EdgeInsets.all(10),
          child: Column(
            children: [
              Image.asset(
                element.image,
                height: 22,
                width: 22,
                fit: BoxFit.fill,
              ),
              10.hGapDp,
              Text(
                element.des,
                style: tsBlack.copyWith(fontSize: 12),
              )
            ],
          ),
        ),
      );

      widgets.add(w);
    });

    return Wrap(
      children: widgets,
    );
  }
}

class _MenuList {
  String image;
  String des;
  Function function;

  _MenuList(this.image, this.des, this.function);
}
